//
//  ZoomImage.m
//  phoneDirectory
//
//  Created by xalo on 16/5/20.
//  Copyright © 2016年 蓝鸥科技有限公司西安分公司. All rights reserved.
//

#import "ZoomImage.h"
#import <UIKit/UIKit.h>

@interface ZoomImage ()<UIScrollViewDelegate>
@property ( nonatomic, strong)UIScrollView *scroll;
@property ( nonatomic, strong)UIPageControl *PageControl;
@end

static CGRect oldframe;
@implementation ZoomImage



+ (void)showImage:(UIImageView *)avatarImageView imageArray:(NSArray *)imageArray{
    
    UIImage *image = avatarImageView.image;
    UIWindow *win = [UIApplication sharedApplication].keyWindow;
    UIView *backgroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    oldframe = [avatarImageView convertRect:avatarImageView.bounds toView:win];
    backgroundView.backgroundColor = [UIColor blackColor];
    backgroundView.alpha =0.5;
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:oldframe];
//    imageView.image = image;
    imageView.tag = 1000;
//    [backgroundView addSubview:imageView];
    [win addSubview:backgroundView];
    
    
    if (imageArray.count > 1) {
        
        ZZScrollView *scrollView = [[ZZScrollView alloc] initWithFrame:CGRectMake(0,100, kWidth, kHeight - 200) imageArray:imageArray];
        [backgroundView addSubview:scrollView];
    }else {
    
        UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 100, kWidth, kHeight-200)];
        [image sd_setImageWithURL:[NSURL URLWithString: imageArray[0]]];
        [backgroundView addSubview:image];
    
    }

    // 点击缩小手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hideImage:)];
    [backgroundView addGestureRecognizer:tap];
    
    [UIView animateWithDuration:0.3 animations:^{
        imageView.frame = CGRectMake(0, ([UIScreen mainScreen].bounds.size.height-image.size.height*[UIScreen mainScreen].bounds.size.width/image.size.width)/2, [UIScreen mainScreen].bounds.size.width, image.size.height * [UIScreen mainScreen].bounds.size.width/image.size.width);
    }];
    
    backgroundView.alpha = 1;
    
}


+ (void)hideImage:(UITapGestureRecognizer *)tap {
    UIView *backgroundView = tap.view;
    UIImageView *imageView = (UIImageView *)[tap.view viewWithTag:1000];
   [UIView animateWithDuration:0.3 animations:^{
       imageView.frame =oldframe;
       backgroundView.alpha = 0;
   }completion:^(BOOL finished) {
       [backgroundView removeFromSuperview];
   }];
    
}

//- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
//    CGPoint offset = self.scroll.contentOffset;
//    int pageCount = offset.x/400;
//    
//}


@end
